//
//  GMShareVC.m
//  Teecloud
//
//  Created by xgm on 2018/12/2.
//  Copyright © 2018 小志. All rights reserved.
//

#import "GMShareVC.h"
#import "GMShareModel.h"
#import "GMShareCell.h"
@interface GMShareVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *texTF;
@property (weak, nonatomic) IBOutlet UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, copy) NSString *searchKey;
@end

@implementation GMShareVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择用户";
    [self addLeftBarButtonWithImage:[UIImage imageNamed:@"left-white-64"] action:@selector(back)];
    [self addRightBarButtonItemWithTitle:@"提交" action:@selector(sumbitBtnClick)];
    [self.tabView registerNib:[UINib nibWithNibName:@"GMShareCell" bundle:nil] forCellReuseIdentifier:@"GMShareCell"];
}
-(void)getData{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:self.searchKey forKey:@"key"];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork HTTPWithMethod:HJRequestMethodPOST url:[NSString stringWithFormat:@"/ajax-api.ashx?$c=TeeyunApp&$m=QueryShareUser&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyNetworkElseCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSArray *tempArr =[GMShareModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:RESULT_NAME_Data]];
                [self.dataArr removeAllObjects];
                [self.dataArr addObjectsFromArray:tempArr];
                [self.tabView reloadData];
            }else{
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:self.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"请求出错" toView:self.view];
        }
    }];
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GMShareCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMShareCell" forIndexPath:indexPath];
    GMShareModel *model = self.dataArr [indexPath.row];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     GMShareModel *model = self.dataArr [indexPath.row];
    if (model.chose) {
         model.chose = NO;
    }else{
         model.chose = YES;
    }
    [self.tabView reloadData];
 
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.searchKey = textField.text;
    [self getData];
}
-(void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)sumbitBtnClick{
    NSMutableString *userids = [[NSMutableString alloc] init];
    for (NSInteger i =0; i<self.dataArr.count; i++) {
         GMShareModel *model = self.dataArr [i];
        if (model.chose) {
             [userids appendFormat:@"%@,",model.UserId];
        }
    }
    if (kIsEmptyString(userids)) {
        [MBProgressHUD showErrorOrSuc:@"请选择分享用户" toView:self.view];
        return;
    }
    NSRange deleteRange = { [userids length] - 1, 1 };
    [userids deleteCharactersInRange:deleteRange];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:self.resId forKey:@"resId"];
    [dict setValue:userids forKey:@"userIds"];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork HTTPWithMethod:HJRequestMethodPOST url:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=PostResInShare&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyNetworkElseCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                 [MBProgressHUD showErrorOrSuc:@"分享成功" toView:self.view];
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                     [self dismissViewControllerAnimated:YES completion:nil];
                 });
                
            }else{
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:self.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"请求出错" toView:self.view];
        }
    }];
    
}
@end
